/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package angrypainters.components;

import angrypainters.application.GameScreenController;
import angrypainters.application.SettingsScreenController;
import angrypainters.application.SplashScreenController;
import angrypainters.application.StartScreenController;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;

/**
 *Class the handles loading the game screens and holding the reference to all resource objects used by other classes
 * @author A7med
 */
public class ResourceManager {

    private Parent splash;
    private NetworkManager nm;
    private Parent main;
    private Parent pause;
    private Parent startScreen = null;
    private Parent settingsScreen = null;
    private ScreensManager sc;
    private SettingsScreenController settingsController;
    private StartScreenController startController;
    private SplashScreenController splashController;
    private ResourceManager instance;
    private GameScreenController gameController;
    private GameModel model;
    private Settings options;

    public Parent getSettingsScreen() {
        if (settingsScreen == null) {
            loader = new FXMLLoader(ResourceManager.class.getResource("/angrypainters/application/SettingsScreen.fxml"));
            System.out.println("loading");
            try {
                settingsScreen = (Parent) loader.load();
                settingsController = loader.getController();
                settingsController.setManager(instance);
                settingsController.setSc(sc);

            } catch (IOException ex) {
                Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return settingsScreen;
    }

    public void setSettingsScreen(Parent settingsScreen) {
        this.settingsScreen = settingsScreen;
    }
    FXMLLoader loader;

    public void animateSplash() {
        splashController.startAnimation();
    }

    public Parent getSplash() {
        if (splash == null) {
            loader = new FXMLLoader(ResourceManager.class.getResource("/angrypainters/application/SplashScreen.fxml"));
            try {
                splash = (Parent) loader.load();
                splashController =loader.getController();
                splashController.setManager(instance);
                splashController.setSc(sc);
            } catch (IOException ex) {
                Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return splash;
    }

    public void setSplash(Parent splash) {
        this.splash = splash;
    }

    public Parent getMain() {
        if (main == null) {
            loader = new FXMLLoader(ResourceManager.class.getResource("/angrypainters/application/GameScreen.fxml"));
            try {
                main = (Parent) loader.load();
                gameController = loader.getController();
                gameController.setManager(instance);
                gameController.setSc(sc);
            } catch (IOException ex) {
                Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return main;
    }

    public void setMain(Parent main) {
        this.main = main;
    }

    public Parent getPause() {
        return pause;
    }

    public void setPause(Parent pause) {
        this.pause = pause;
    }

    public Parent getStartScreen() {
        if (startScreen == null) {
            loader = new FXMLLoader(ResourceManager.class.getResource("/angrypainters/application/StartScreen.fxml"));
            System.out.println("loading");
            try {
                startScreen = (Parent) loader.load();
                startController = loader.getController();
                startController.setManager(instance);
                startController.setSc(sc);

            } catch (IOException ex) {
                Logger.getLogger(ResourceManager.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return startScreen;
    }

    public void setStartScreen(Parent startScreen) {
        this.startScreen = startScreen;
    }

    public ScreensManager getSc() {
        return sc;
    }

    public void setSc(ScreensManager sc) {
        this.sc = sc;
    }

    public SettingsScreenController getSettingsController() {
        return settingsController;
    }

    public void setSettingsController(SettingsScreenController settingsController) {
        this.settingsController = settingsController;
    }

    public StartScreenController getStartController() {
        return startController;
    }

    public void setStartController(StartScreenController startController) {
        this.startController = startController;
    }

    public ResourceManager getInstance() {
        return instance;
    }

    public void setInstance(ResourceManager instance) {
        this.instance = instance;
    }

    public SplashScreenController getSplashController() {
        return splashController;
    }

    public void setSplashController(SplashScreenController splashController) {
        this.splashController = splashController;
    }

    public GameScreenController getGameController() {
        return gameController;
    }

    public void setGameController(GameScreenController gameController) {
        this.gameController = gameController;
    }

    public GameModel getModel() {
        return model;
    }

    public void setModel(GameModel model) {
        this.model = model;
    }

    public NetworkManager getNm() {
        return nm;
    }

    public void setNm(NetworkManager nman) {
        this.nm = nman;
        getMain();
        
        nm.setGameController(gameController);
        nm.setStartController(startController);
        gameController.setNetManager(nm);
        gameController.setModel(getModel());
        startController.setNetManager(nm);
        startController.setOption(options);

    }

    public Settings getOptions() {
        return options;
    }

    public void setOptions(Settings options) {
        this.options = options;
    }

}
